package com.xd.core.api.controller;

import com.xd.common.ascept.CheckSign;
import com.xd.common.ascept.ResponseNotIntercept;
import com.xd.common.exception.XkdException;
import com.xd.core.api.dto.*;
import com.xd.core.api.service.ApiService;
import com.xd.core.api.http.Signer;
import com.xd.core.api.http.Verifier;
import com.xd.core.api.vo.BalanceVo;
import com.xd.core.api.vo.TowBalanceVo;
import com.xd.core.company.dto.RegisterDto;
import com.xd.core.company.entity.BindBank;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

@Api(tags = "第三方API")
@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    private ApiService apiService;


    /*
    * 图片支持jpg，png
1身份证正面:文件名101,小于5M(必填)
2身份证反面:文件名102,小于5M(必填)
3 营业执照:文件名104,小于5M(必填)
4银行许可证:文件名401,小于5M(必填)。
5 合作协议：601(非必填)
压缩zip文件
    * */
    @ApiOperation("资质文件上传")
    @PostMapping("/upload/{companyId}")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "companyId", value = "资方ID", dataType = "int")
    })
    public Map<String, String> upload(MultipartFile file, @PathVariable Integer companyId) throws Exception {
        return apiService.upload(file, companyId);
    }


    //收益账户与预存款账户合一
    @PreAuthorize("hasAnyAuthority('sys:Authentication')")
    @CheckSign
    @ApiOperation("机构注册预存款电子账户")
    @PostMapping("/prestoreRegister")
    public void prestoreRegister(@RequestBody RegisterDto registerDto) throws IOException {
        apiService.prestoreRegister(registerDto);
    }

    public static void main(String[] args) throws IOException {
//        String cerPath = System.getProperty("user.dir") + "/resources/client_pub.cer";
//        String cerPath = System.getProperty("user.dir")+"/client_pub.cer";
        String cerPath = "D:\\companyProject\\XDproject\\src\\main\\resources\\client_pub.cer";
        System.out.println(cerPath);
        String data = "eyJhY2NvdW50X25vIjoiNjgwNjAxMDAwNTYyNDMiLCJhdWRpdF9pbmZvIjoi6ZO26KGM5byA5oi35oiQ5YqfIiwiYXVkaXRfc3RhdHVzIjoiU1VDQ0VTUyIsImF1ZGl0X3RpbWUiOiIyMDI1MDMwNDE1MTYzNyIsImJ1c2luZXNzX2xpY2Vuc2UiOiI5MTM3MDIwNTMyNTk4MjAzNUoiLCJmdWxsX25hbWUiOiLmtYvor5XkvIHkuJrlm57osIMiLCJodEFjY291bnRfbm8iOiI5MTAwMDAxMTAxMDAwMDE0MDU5Iiwid2FsbGV0X25vIjoiMjI1MDMwNDAwMDAwMDQ1MCJ9";

        TreeMap<String, String> mapResult = new TreeMap<>();
        mapResult.put("app_id", "202309121919");
        mapResult.put("sign_type", "RSA");
        mapResult.put("client_trans_id", "T20250304150956029");
        mapResult.put("trans_timestamp", "20250304151637");
        mapResult.put("trans_type", "2903");
        mapResult.put("callback_url", "http://121.40.183.61:80/xd/api/prestoreRegisterResult");
        mapResult.put("version", "1.0.0");
        mapResult.put("data", data);
//        mapResult = JXMConvertUtil.JsonConvertHashMap(mapResult);// 将响应结果转化为TreeMap对象。
        String sign = "03b6d9cfbecc157f3e190b84fa1690409f8b22765715040b24d99f0331758adb16b7fb112367a76a8d272b196866f601a3d760167b443d78fdb2c37aeab7eb2422b97758899c394fb79961423d4ab146526fb3c1d8b903cd88ad12abe35ada503a72c1f7d26d285cf430d560fe2c8794b57309205d20f69301383d200cda7751ea4dad1b2d2642f5f70735dd75443a9bd49c45999ae708ec155d3b29846d02dcd832f79e71fca710182ed811ff9941098875e55d6ebcf7bd086a662105256c023c257b4a41dde6739718d6569ba2908a9abcd1fadb0d0ecb7a770e9482e4be3b220179c43d62db4114b4456acbaf46cf3a0410fa269ce930b979cb122a047bd2";
        System.out.println(mapResult);
        String signStr = Signer.concatTreeMapParam(mapResult);//生成待验签字符串
        System.out.println(signStr);
        Boolean bool = Verifier.verifyByCerFile(signStr, sign, cerPath);
        System.out.println(bool);
        String result = Signer.Base64Decode(data);
        System.out.println(result);
        JSONObject jsonObject = JSONObject.fromObject(result);
        System.out.println(jsonObject.getString("audit_info"));

    }


    //机构注册流水号查询
    @ApiOperation("机构注册查询")
    @GetMapping("/getRegisterStatus")
    public String getRegisterStatus(String clientTransId) throws IOException {
        if (StringUtils.isBlank(clientTransId))
            throw new XkdException("请先申请开户");
        JSONObject JSON = apiService.getRegisterStatus(clientTransId);
        return JSON.getString("audit_info");
    }

    @CheckSign
    @ApiOperation("查询银行电子账户状态")
    @PostMapping("/getAccountStatus")
    public String getAccountStatus(@RequestBody ClientTransDto clientTransDto) throws IOException {
        return apiService.getAccountStatus(clientTransDto.getWalletNo());
    }

    //提现到宝付余额 再提现
    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Withdrawal')")
    @CheckSign
    @ApiOperation("调拨提现")
    @PostMapping("/allot")
    public void allot(@RequestBody PrestoreWithdrawDepositDto prestoreWithdrawDepositDto) throws IOException {
        apiService.allot(prestoreWithdrawDepositDto);
    }

    //传渠道一银行卡ID
    //直接宝付余额提现
    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Withdrawal')")
    @CheckSign
    @ApiOperation("宝付提现")
    @PostMapping("/baofuwithdrawDeposit")
    public void baofuwithdrawDeposit(@RequestBody PrestoreWithdrawDepositDto prestoreWithdrawDepositDto) throws IOException {
        apiService.baofuwithdrawDeposit(prestoreWithdrawDepositDto);
    }



//    @ApiOperation("提现到对公账户")
//    @PostMapping("/withdrawDeposit")
//    public void withdrawDeposit(String allot_client_trans_id) throws IOException {
//        apiService.withdrawDeposit(allot_client_trans_id);
//    }


    /*
    * 电子账户余额
    * */
    @CheckSign
    @ApiOperation("余额查询")
    @PostMapping("/balance")
    public BalanceVo balance(@RequestBody ClientTransDto clientTransDto) throws IOException {
        return apiService.balance(clientTransDto.getWalletNo(),clientTransDto.getType());
    }

    /*
    * 资方通道二余额查询 1-放款账户，2-回款账户
    * */
    @CheckSign
    @ApiOperation("资方通道二余额查询")
    @PostMapping("/towBalance")
    public TowBalanceVo towBalance(@RequestBody TowBalanceDto towBalanceDto) {
        return apiService.towBalance(towBalanceDto);
    }
    @ApiOperation("平台通道二回款账户余额查询")
    @PostMapping("/PTtowBalance")
    public TowBalanceVo PTtowBalance() {
        return apiService.PTtowBalance();
    }



    @CheckSign
    @ApiOperation("转账订单查询")
    @PostMapping("/getTransferOrder")
    public HashMap<String, String> getTransferOrder(@RequestBody ClientTransDto clientTransDto) throws IOException {
        return apiService.getTransferOrder(clientTransDto.getClientTransId());
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Payment','sys:BusinessAccount-DepositRefund')")
    @CheckSign
    @ApiOperation("创建电子转账订单")
    @PostMapping("/transferAccounts")
    public HashMap<String, String> transferAccounts(@RequestBody TransferDto transferDto) throws IOException {
        return apiService.transferAccounts(transferDto);
    }
    @PreAuthorize("hasAnyAuthority('sys:BusinessAccount-Recharge')")
    @CheckSign
    @ApiOperation("通道二线下充值")
    @PostMapping("/towRecharge")
    public void towRecharge(@RequestBody TowRechargeDto towRechargeDto) throws Exception {
         apiService.towRecharge(towRechargeDto);
    }

    @CheckSign
    @ApiOperation("企业绑卡(线下对公打款需要绑定白名单)")
    @PostMapping("/bindBankCard")
    public void bindBankCard(@RequestBody BindBank bindBank) throws IOException {
        apiService.bindBankCard(bindBank);
    }

}
